var planeinfo_href=window.location.href;
$(document).ready(function(){
    var menu_height=$(document).height();
    $("#left-menu").css("height",menu_height+"px");
    var href_info=planeinfo_href.split("?")[1];
    var href_infoall=href_info.split("&");
    for(var i=0;i<href_infoall.length;i++){
        if(href_infoall[i].split("=")[0]=="plane_type"){
            if(href_infoall[i].split("=")[1]=="unic"){
                $("#planepage_unic_wrap").css("display","block");
                $("#planepage_tfa_wrap").css("display","none");
                //add_planeinfo_after();
                //get_plane_unic();
                
            }
            if(href_infoall[i].split("=")[1]=="tfa"){
                $("#planepage_unic_wrap").css("display","none");
                $("#planepage_tfa_wrap").css("display","block");
                //add_planeinfo_after();
                //get_plane_tfa();
            }
            get_plane_unic();
        }
        if(href_infoall[i].split("=")[0]=="type"){
            if(href_infoall[i].split("=")[1]=="add"){
                $("#tfalist_wrap").css("display","none");
                $("#tfaadd_wrap").css("display","block");
                $("#tfainfo_wrap").css("display","none");
            }
            if(href_infoall[i].split("=")[1]=="info"){
                $("#tfalist_wrap").css("display","none");
                $("#tfaadd_wrap").css("display","none");
                $("#tfainfo_wrap").css("display","block");
            }
            if(href_infoall[i].split("=")[1]=="list"){
                $("#tfalist_wrap").css("display","block");
                $("#tfaadd_wrap").css("display","none");
                $("#tfainfo_wrap").css("display","none");
            }
        }
         if(href_infoall[i].split("=")[0]=="type"){
            if(href_infoall[i].split("=")[1]=="add"){
                $("#uniclist_wrap").css("display","none");
                $("#unicadd_wrap").css("display","block");
                $("#unicinfo_wrap").css("display","none");
            }
            if(href_infoall[i].split("=")[1]=="info"){
                $("#uniclist_wrap").css("display","none");
                $("#unicadd_wrap").css("display","none");
                $("#unicinfo_wrap").css("display","block");
            }
            if(href_infoall[i].split("=")[1]=="list"){
                $("#uniclist_wrap").css("display","block");
                $("#unicadd_wrap").css("display","none");
                $("#unicinfo_wrap").css("display","none");
            }
        }
        
    }
    $(".change_planeinfo input").attr("disabled","disabled");
    $(".change_planeinfo select").attr("disabled","disabled");
    $(".change_planeinfo input").css("background","#fff");
    $(".change_planeinfo input").css("border","0");
    // $("#planepage_unic").click(function(){
    //     $("#planepage_unic_wrap").css("display","block");
    //     $("#planepage_tfa_wrap").css("display","none");
    // });
    // $("#planepage_tfa").click(function(){
    //     $("#planepage_unic_wrap").css("display","none");
    //     $("#planepage_tfa_wrap").css("display","block");
    // });
    
});
$("#addplane_cancel_unic").click(function(){
    location.href="/plane_info?plane_type=unic&type=list";
});
$("#addplane_confirm_unic").click(function(){
    //uavnumber, type, factory_time, flight_location, aftermarket_name
    var add_uavnumber_unic=$("#add_uavnumber_unic").val();
    var add_unic_type="UnicornA";
    var add_factory_unic=$("#addyear_factory_unic").val()+"."+$("#addmouth_factory_unic").val();
    var add_location_unic=$("#add_location_unic").val();
    var add_planeafter_unic=$("#add_planeafter_unic option:selected").text();
    post_plane_add(add_uavnumber_unic,add_unic_type,add_factory_unic,add_location_unic,add_planeafter_unic);
    location.href="/plane_info?plane_type=unic&type=list";
});
$("#readplane_change_unic").click(function(){
    $("#readplane_change_unic").css("display","none");
    $("#readplane_cancel_unic").css("display","block");
    $("#readplane_confirm_unic").css("display","block");
    $(".change_planeinfo input,select").attr("disabled",false);
    $(".change_planeinfo input,select").css("background","#fff");
    $(".change_planeinfo input").css("border","1px solid #ccc");
    $(".read_afterinfo").css("display","none");
});
$("#readplane_cancel_unic").click(function(){
    $("#readplane_change_unic").css("display","block");
    $("#readplane_cancel_unic").css("display","none");
    $("#readplane_confirm_unic").css("display","none");
    $(".change_planeinfo input,select").attr("disabled","disabled");
    $(".change_planeinfo input,select").css("background","#fff");
    $(".change_planeinfo input").css("border","0");
    $(".read_afterinfo").css("display","block");
});
$("#readplane_confirm_unic").click(function(){
    //unic改变确认

    //location.href="/plane_info?plane_type=unic&type=list";
});

$("#addplane_cancel_tfa").click(function(){
    location.href="/plane_info?plane_type=tfa&type=list";
});
$("#addplane_confirm_tfa").click(function(){
    //uavnumber, type, factory_time, flight_location, aftermarket_name
    var add_uavnumber_tfa=$("#add_uavnumber_tfa").val();
    var add_tfa_type="tfa";
    var add_factory_tfa=$("#addyear_factory_tfa").val()+"."+$("#addmouth_factory_tfa").val();
    var add_location_tfa=$("#add_location_tfa").val();
    var add_planeafter_tfa=$("#add_planeafter_tfa option:selected").text();
    post_plane_add(add_uavnumber_tfa,add_tfa_type,add_factory_tfa,add_location_tfa,add_planeafter_tfa);
    location.href="/plane_info?plane_type=tfa&type=list";
});
$("#readplane_change_tfa").click(function(){
    $("#readplane_change_tfa").css("display","none");
    $("#readplane_cancel_tfa").css("display","block");
    $("#readplane_confirm_tfa").css("display","block");
    $(".change_planeinfo input,select").attr("disabled",false);
    $(".change_planeinfo input,select").css("background","#fff");
    $(".change_planeinfo input").css("border","1px solid #ccc");
    $(".read_afterinfo").css("display","none");
});
$("#readplane_cancel_tfa").click(function(){
    $("#readplane_change_tfa").css("display","block");
    $("#readplane_cancel_tfa").css("display","none");
    $("#readplane_confirm_tfa").css("display","none");
    $(".change_planeinfo input,select").attr("disabled","disabled");
    $(".change_planeinfo input,select").css("background","#fff");
    $(".change_planeinfo input").css("border","0");
    $(".read_afterinfo").css("display","block");
});
$("#readplane_confirm_tfa").click(function(){
    //tfa改变确认

    //location.href="/plane_info?plane_type=tfa&type=list";
});

//左侧菜单始终保持100%高度
$(document).scroll(function(){
    var menu_height=$(document).height();
    $("#left-menu").css("height",menu_height+"px");
});

function topbtn(){
    if($("#topbar-ul").css("display")=="none"){
        $("#topbar-ul").css("display","block");
    }else{
        $("#topbar-ul").css("display","none");
    }
     
}
function add_planeinfo_after(){
    $.ajax({
        url: '/aftermarket_list',
        type: 'get',
        success: function(data,status){
            if(status == 'success'){
                console.log(data);
                for(var i in data){
                    $("#add_planeafter_unic").append("<option value='"+i+"'>"+data[i].name+"</option>");
                    $("#read_planeafter_unic").append("<option value='"+i+"'>"+data[i].name+"</option>");
                    $("#add_planeafter_tfa").append("<option value='"+i+"'>"+data[i].name+"</option>");
                    $("#read_planeafter_tfa").append("<option value='"+i+"'>"+data[i].name+"</option>");
                }
                 add_planeinfo_unic();
                 add_planeinfo_tfa();
            }
        },
        error: function(data,err){
            console.log(data);
        }
    });
}
function get_plane_unic(){
    $.ajax({
        url: '/plane_list',
        type: 'get',
        success: function(data,status){
            if(status == 'success'){
                //console.log(data);
                var str="";
                var str1='<a class="add-planeinfo" href="/plane_info?plane_type=unic&type=add">+添加飞机</a>';
                var num1=1;
                var num2=1;
                var str3="";
                var str4='<a class="add-planeinfo" href="/plane_info?plane_type=tfa&type=add">+添加飞机</a>';
                for(var i in data){
                    if(data[i].type=="UnicornA"){
                        str+="<ul><li>"+num1+"</li><li>"+data[i].uavnumber+"</li><li>出厂时间 "+data[i].factory_time.split(".")[0]+"-"+data[i].factory_time.split(".")[1]+"</li><li>售后人员："+data[i].aftermarket_name+"</li><li><a href='/plane_info?plane_type=unic&type=info&id="+data[i]._id+"'>详情</a></li></ul>";
                        num1++;
                    }
                    if(data[i].type=="tfa"){
                        str3+="<ul><li>"+num2+"</li><li>"+data[i].uavnumber+"</li><li>出厂时间 "+data[i].factory_time.split(".")[0]+"-"+data[i].factory_time.split(".")[1]+"</li><li>售后人员："+data[i].aftermarket_name+"</li><li><a href='/plane_info?plane_type=tfa&type=info&id="+data[i]._id+"'>详情</a></li></ul>";
                        num2++;
                    }
                    
                }
                $("#uniclist_wrap").html(str+str1);
                $("#tfalist_wrap").html(str3+str4);
                add_planeinfo_after();
            }
        },
        error: function(data,err){
            console.log(data);
        }
    });
}
function add_planeinfo_unic(){
    $.ajax({
        url: '/plane_list',
        type: 'get',
        success: function(data,status){
            if(status == 'success'){
                console.log(data);
                var href_infoall=planeinfo_href.split("?")[1].split("&");
                for(var i in data){
                    for(var j in href_infoall){
                        if(href_infoall[j].split("=")[0]=="id"){
                            if(href_infoall[j].split("=")[1]==data[i]._id){
                                $("#read_uavnumber_unic").val(data[i].uavnumber);
                                $("#readyear_factory_unic").val(data[i].factory_time.split(".")[0]);
                                $("#readmouth_factory_unic").val(data[i].factory_time.split(".")[1]);
                                $("#read_location_unic").val(data[i].flight_location);
                                var aftermarket_name=data[i].aftermarket_name;
                                var read_options=$("#read_planeafter_unic option");
                                for(var i in read_options){
                                    if(aftermarket_name==read_options[i].text){
                                        read_options[i].selected=true;
                                        read_afterinfo_unic(read_options[i].text);
                                    };
                                }
                            }   
                        }
                    }
                }
            }
        },
        error: function(data,err){
            console.log(data);
        }
    });
}
function read_afterinfo_unic(name){
    $.ajax({
        url: '/aftermarket_list',
        type: 'get',
        success: function(data,status){
            if(status == 'success'){
                console.log(data);
                for(var i in data){
                    if(data[i].name==name){
                        $(".read_afterphone_unic").val(data[i].phone);
                        $(".read_afteremail_unic").val(data[i].email);
                    }
                }
            }
        },
        error: function(data,err){
            console.log(data);
        }
    });
}


// function get_plane_tfa(){
//     $.ajax({
//         url: '/plane_list',
//         type: 'get',
//         success: function(data,status){
//             if(status == 'success'){
//                 //console.log(data);
//                 var str="";
//                 var str1='<a class="add-planeinfo" href="/plane_info?plane_type=tfa&type=add">+添加飞机</a>';
//                 for(var i in data){
//                     if(data[i].type=="tfa"){
//                         str+="<ul><li>"+(parseInt(i)+1)+"</li><li>"+data[i].uavnumber+"</li><li>出厂时间 "+data[i].factory_time.split(".")[0]+"-"+data[i].factory_time.split(".")[1]+"</li><li>售后人员："+data[i].aftermarket_name+"</li><li><a href='/plane_info?plane_type=tfa&type=info&id="+data[i]._id+"'>详情</a></li></ul>";
                        
//                     }
//                 }
//                 $("#tfalist_wrap").html(str+str1);
//                 add_planeinfo_after();
//             }
//         },
//         error: function(data,err){
//             console.log(data);
//         }
//     });
// }
function add_planeinfo_tfa(){
    $.ajax({
        url: '/plane_list',
        type: 'get',
        success: function(data,status){
            if(status == 'success'){
                console.log(data);
                var href_infoall=planeinfo_href.split("?")[1].split("&");
                for(var i in data){
                    for(var j in href_infoall){
                        if(href_infoall[j].split("=")[0]=="id"){
                            if(href_infoall[j].split("=")[1]==data[i]._id){
                                $("#read_uavnumber_tfa").val(data[i].uavnumber);
                                $("#readyear_factory_tfa").val(data[i].factory_time.split(".")[0]);
                                $("#readmouth_factory_tfa").val(data[i].factory_time.split(".")[1]);
                                $("#read_location_tfa").val(data[i].flight_location);
                                var aftermarket_name=data[i].aftermarket_name;
                                var read_options=$("#read_planeafter_tfa option");
                                for(var i in read_options){
                                    console.log(read_options[i].text);
                                    if(aftermarket_name==read_options[i].text){
                                        read_options[i].selected=true;
                                        read_afterinfo_unic(read_options[i].text);
                                    };
                                }
                            }   
                        }
                    }
                }
            }
        },
        error: function(data,err){
            console.log(data);
        }
    });
}